home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / CMPINT.H < prev    next >
Text File  |  1990-06-20  |  9KB  |  261 lines

  1. /* -*-C-*-
  2.  
  3. $Header: cmpint.h,v 10.4 90/06/20 17:39:09 GMT cph Exp $
  4.  
  5. Copyright (c) 1987, 1988, 1989, 1990 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Macros for the interface between compiled code and interpreted code. */
  36.  
  37. /* Stack Gap Operations: */
  38.  
  39. /* With_Stack_Gap opens a gap Gap_Size wide Gap_Position cells above the
  40.  * top of the stack.  Code must push Gap_Size objects.  It executes Code
  41.  * with the stack pointer placed so that these objects will fill the gap.
  42.  */
  43.  
  44. #define With_Stack_Gap(Gap_Size, Gap_Position, Code)            \
  45. {                                    \
  46.   fast long size_to_move = (Gap_Position);                \
  47.   fast SCHEME_OBJECT * Destination = (STACK_LOC (- (Gap_Size)));    \
  48.   SCHEME_OBJECT * Saved_Destination = Destination;            \
  49.   while ((--size_to_move) >= 0)                        \
  50.     (STACK_LOCATIVE_POP (Destination)) = (STACK_POP ());        \
  51.   Code;                                    \
  52.   Stack_Pointer = Saved_Destination;                    \
  53. }
  54.  
  55. /* Close_Stack_Gap closes a gap Gap_Size wide Gap_Position cells above the
  56.  * top of the stack.  The contents of the gap are lost.
  57.  */
  58.  
  59. #define Close_Stack_Gap(Gap_Size, Gap_Position, extra_code)        \
  60. {                                    \
  61.   fast long size_to_move;                        \
  62.   fast SCHEME_OBJECT *Source;                        \
  63.                                     \
  64.   size_to_move = (Gap_Position);                    \
  65.   Source = (STACK_LOC (size_to_move));                    \
  66.   Stack_Pointer = (STACK_LOC ((Gap_Size) + size_to_move));        \
  67.   extra_code;                                \
  68.   while (--size_to_move >= 0)                        \
  69.   {                                    \
  70.     STACK_PUSH (STACK_LOCATIVE_PUSH (Source));                \
  71.   }                                    \
  72. }
  73.  
  74. /* Going from interpreted code to compiled code */
  75.  
  76. /* Tail recursion is handled as follows:
  77.    if the return code is `reenter_compiled_code', it is discarded,
  78.    and the two contiguous interpreter segments on the stack are
  79.    merged.
  80.  */
  81.  
  82. /* Apply interface:
  83.    calling a compiled procedure with a frame nslots long.
  84.  */
  85.  
  86. #define apply_compiled_setup(nslots)                    \
  87. {                                    \
  88.   long frame_size;                            \
  89.                                     \
  90.   frame_size = (nslots);                        \
  91.   if (STACK_REF(frame_size + CONTINUATION_RETURN_CODE) ==        \
  92.       (MAKE_OBJECT (TC_RETURN_CODE, RC_REENTER_COMPILED_CODE)))        \
  93.   {                                    \
  94.     /* Merge compiled code segments on the stack. */            \
  95.     Close_Stack_Gap (CONTINUATION_SIZE,                    \
  96.              frame_size,                    \
  97.            {                            \
  98.              long segment_size =                \
  99.                (OBJECT_DATUM                    \
  100.             (STACK_REF                    \
  101.              (CONTINUATION_EXPRESSION -            \
  102.               CONTINUATION_SIZE)));                \
  103.              last_return_code = (STACK_LOC (segment_size));    \
  104.            });                            \
  105.     /* Undo the subproblem rotation. */                    \
  106.     Compiler_End_Subproblem();                        \
  107.   }                                    \
  108.   else                                    \
  109.   {                                    \
  110.     /* Make a new compiled code segment which includes this frame. */    \
  111.     /* History need not be hacked here. */                \
  112.     With_Stack_Gap(1,                            \
  113.            frame_size,                        \
  114.          {                            \
  115.            last_return_code = (STACK_LOC (0));            \
  116.            STACK_PUSH (return_to_interpreter);            \
  117.          });                            \
  118.   }                                    \
  119. }
  120.  
  121. /* Eval interface:
  122.    executing a compiled expression.
  123.  */
  124.  
  125. #define execute_compiled_setup()                    \
  126. {                                    \
  127.   if (STACK_REF(CONTINUATION_RETURN_CODE) ==                \
  128.       (MAKE_OBJECT (TC_RETURN_CODE, RC_REENTER_COMPILED_CODE)))        \
  129.   {                                    \
  130.     /* Merge compiled code segments on the stack. */            \
  131.     long segment_size;                            \
  132.                                     \
  133.     Restore_Cont();                            \
  134.     segment_size = OBJECT_DATUM (Fetch_Expression());            \
  135.     last_return_code = (STACK_LOC (segment_size));            \
  136.     /* Undo the subproblem rotation. */                    \
  137.     Compiler_End_Subproblem();                        \
  138.   }                                    \
  139.     else                                \
  140.   {                                    \
  141.     /* Make a new compiled code segment on the stack. */        \
  142.     /* History need not be hacked here. */                \
  143.     last_return_code = (STACK_LOC (0));                    \
  144.     STACK_PUSH (return_to_interpreter);                    \
  145.   }                                    \
  146. }
  147.  
  148. /* Pop return interface:
  149.    Returning to compiled code from the interpreter.
  150.  */
  151.  
  152. #define compiled_code_restart()                        \
  153. {                                    \
  154.   long segment_size = OBJECT_DATUM (Fetch_Expression());        \
  155.   last_return_code = (STACK_LOC (segment_size));            \
  156.   /* Undo the subproblem rotation. */                    \
  157.   Compiler_End_Subproblem();                        \
  158. }
  159.  
  160. /* Going from compiled code to interpreted code */
  161.  
  162. /* Tail recursion is handled in the following way:
  163.    if the return address is `return_to_interpreter', it is discarded,
  164.    and the two contiguous interpreter segments on the stack are
  165.    merged.
  166.  */
  167.  
  168. /* Apply interface:
  169.    calling an interpreted procedure (or unsafe primitive)
  170.    with a frame nslots long.
  171.  */
  172.  
  173. #define compiler_apply_procedure(nslots)                \
  174. {                                    \
  175.   long frame_size = (nslots);                        \
  176.   if ((STACK_REF (frame_size)) == return_to_interpreter)        \
  177.   {                                    \
  178.     Close_Stack_Gap(1, frame_size, {});                    \
  179.     /* Set up the current rib. */                    \
  180.     Compiler_New_Reduction ();                        \
  181.   }                                    \
  182.   else                                    \
  183.     { /* Make a new interpreter segment which includes this frame. */    \
  184.       With_Stack_Gap                            \
  185.     (CONTINUATION_SIZE,                        \
  186.      frame_size,                            \
  187.      {                                \
  188.        long segment_size =                        \
  189.          (STACK_LOCATIVE_DIFFERENCE                    \
  190.           (last_return_code, (STACK_LOC (0))));            \
  191.        Store_Expression (LONG_TO_UNSIGNED_FIXNUM (segment_size));    \
  192.        Store_Return (RC_REENTER_COMPILED_CODE);            \
  193.        Save_Cont ();                        \
  194.      });                                \
  195.       /* Rotate history to a new subproblem. */                \
  196.       Compiler_New_Subproblem ();                    \
  197.     }                                    \
  198. }
  199.  
  200. /* Pop Return interface:
  201.    returning to the interpreter from compiled code.
  202.    Nothing needs to be done at this time.
  203.  */
  204.  
  205. #define compiled_code_done()
  206.  
  207. /* Various handlers for backing out of compiled code. */
  208.  
  209. /* Backing out of apply. */
  210.  
  211. #define apply_compiled_backout()                    \
  212. {                                    \
  213.   compiler_apply_procedure(STACK_ENV_EXTRA_SLOTS +            \
  214.                OBJECT_DATUM (STACK_REF (STACK_ENV_HEADER)));\
  215. }
  216.  
  217. /* Backing out of eval. */
  218.  
  219. #define execute_compiled_backout()                    \
  220. {                                    \
  221.   if ((STACK_REF (0)) == return_to_interpreter)                \
  222.   {                                    \
  223.     /* Set up the current rib. */                    \
  224.     Compiler_New_Reduction ();                        \
  225.   }                                    \
  226.   else                                    \
  227.   {                                    \
  228.     long segment_size =                            \
  229.       (STACK_LOCATIVE_DIFFERENCE (last_return_code, (STACK_LOC (0))));    \
  230.     Store_Expression (LONG_TO_UNSIGNED_FIXNUM (segment_size));        \
  231.     Store_Return (RC_REENTER_COMPILED_CODE);                \
  232.     Save_Cont ();                            \
  233.     /* Rotate history to a new subproblem. */                \
  234.     Compiler_New_Subproblem ();                        \
  235.   }                                    \
  236. }
  237.  
  238. /* Backing out because of special errors or interrupts.
  239.    The microcode has already setup a return code with a #F.
  240.    No tail recursion in this case.
  241.    ***
  242.        Is the history manipulation correct?
  243.        Does Microcode_Error do something special?
  244.    ***
  245.  */
  246.  
  247. #define compiled_error_backout()                    \
  248. {                                    \
  249.   long segment_size;                            \
  250.                                     \
  251.   Restore_Cont();                            \
  252.   segment_size =                            \
  253.     (STACK_LOCATIVE_DIFFERENCE (last_return_code, (STACK_LOC (0))));    \
  254.   Store_Expression (LONG_TO_UNSIGNED_FIXNUM (segment_size));        \
  255.   /* The Store_Return is a NOP, the Save_Cont is done by the code    \
  256.      that follows. */                            \
  257.   /* Store_Return (OBJECT_DATUM (Fetch_Return ())); */            \
  258.   /* Save_Cont (); */                            \
  259.   Compiler_New_Subproblem ();                        \
  260. }
  261.